package com.fbt.transfer.app.driver.ui.me;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.fbt.transfer.app.driver.http.AbstractCallback;
import com.fbt.transfer.app.driver.http.ApiClient;
import com.fbt.transfer.app.driver.model.ProfileInfo;
import com.fbt.transfer.app.driver.model.Result;
import com.fbt.transfer.app.driver.util.AppContextProvider;
import retrofit2.Callback;

public class MeViewModel extends ViewModel {

    private final MutableLiveData<String> mText;
    private final MutableLiveData<ProfileInfo> mProfileInfo;
    private final MutableLiveData<Boolean> mIsLoading;
    private final MutableLiveData<String> mErrorMessage;

    public MeViewModel() {
        mText = new MutableLiveData<>();
        mText.setValue("这是我的页面");
        
        mProfileInfo = new MutableLiveData<>();
        mIsLoading = new MutableLiveData<>(false);
        mErrorMessage = new MutableLiveData<>();
        
        // 加载用户信息
        loadProfileInfo();
    }

    public LiveData<String> getText() {
        return mText;
    }
    
    public LiveData<ProfileInfo> getProfileInfo() {
        return mProfileInfo;
    }
    
    public LiveData<Boolean> getIsLoading() {
        return mIsLoading;
    }
    
    public LiveData<String> getErrorMessage() {
        return mErrorMessage;
    }
    
    public void loadProfileInfo() {
        mIsLoading.setValue(true);
        mErrorMessage.setValue(null);

    ApiClient.getApiInterface()
        .personInfo()
        .enqueue(
            new AbstractCallback<ProfileInfo>(AppContextProvider.getAppContext(), true) {
              @Override
              protected void onSuccess(ProfileInfo data) {
                mProfileInfo.setValue(data);
              }
            });
    }
    
    // 退出登录
    public void logout(Callback<Result<Boolean>> callback) {
        ApiClient.getApiInterface().logout().enqueue(callback);
    }
}
